home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Magazine / C_Tutorial / Part-11 / req3 / idcmp.c < prev    next >
C/C++ Source or Header  |  1998-04-05  |  11KB  |  414 lines

  1. #include "idcmp.h"
  2. #include "drawwin.h"
  3. #include "gadgets.h"
  4. #include "loadsave.h"
  5. #include "menu.h"
  6. #include "toolwin.h"
  7. #include "arexx.h"
  8. #include "fractal.h"
  9.  
  10. #include<dos/rdargs.h>
  11.  
  12. #include<string.h>
  13. #include<stdio.h>
  14.  
  15. #include<clib/dos_protos.h>
  16. #include<clib/exec_protos.h>
  17. #include<clib/gadtools_protos.h>
  18. #include<clib/graphics_protos.h>
  19. #include<clib/intuition_protos.h>
  20.  
  21. #define PROGRAM "HelloPainter"
  22. #define VERSION "11.3"
  23.  
  24. static UBYTE* VersionString = "\0$VER: " PROGRAM " " VERSION;
  25.  
  26. static void doGadgetUp(struct Window*, UWORD, struct Gadget*);
  27. static int  doMenuPick(struct Window*, UWORD);
  28. static int  doARexx(struct RexxMsg*, struct Window*);
  29.  
  30. static void new(struct Window*);
  31.  
  32. static int  isCommand(char*, char*, char*, LONG*);
  33. static void freeCommand(void);
  34.  
  35. /* Our message handling code */
  36. void handleIDCMP()
  37. {
  38.     char* text = "Hello World!";
  39.     int going = TRUE;
  40.     int drawing = FALSE;
  41.     ULONG drawsig, toolsig, arexxsig, gotsig;
  42.     struct Window* drawwin = getDrawWin();
  43.     drawsig = 1 << drawwin->UserPort->mp_SigBit;
  44.     arexxsig = getARexxSig();
  45.     while(going)
  46.     {
  47.         struct IntuiMessage* intuimsg;
  48.         /* Only include tool window signal mask if window is open */
  49.         toolsig = getToolSig();
  50.         /* Wait for messages to arrive */
  51.         gotsig = Wait(drawsig | toolsig | arexxsig);
  52.         /* Messages have arrived: loop through all of them */
  53.         /* Check messages from the drawing window first */
  54.         if(gotsig & drawsig)
  55.         {
  56.             while(intuimsg = GT_GetIMsg(drawwin->UserPort))
  57.             {
  58.                 /* Copy the important bits of the message */
  59.                 ULONG class = intuimsg->Class;
  60.                 UWORD code = intuimsg->Code;
  61.                 WORD mousex = intuimsg->MouseX;
  62.                 WORD mousey = intuimsg->MouseY;
  63.                 /* Reply when finished copying bits from message */
  64.                 GT_ReplyIMsg(intuimsg);
  65.                 /* Act on this message... */
  66.                 switch(class)
  67.                 {
  68.                 case IDCMP_MOUSEBUTTONS:
  69.                     switch(code)
  70.                     {
  71.                     case SELECTDOWN:
  72.                         drawing = TRUE;
  73.                         break;
  74.                     case SELECTUP:
  75.                         drawing = FALSE;
  76.                         break;
  77.                     }
  78.                     /* break; omitted so we draw on click, too */
  79.                 case IDCMP_MOUSEMOVE:
  80.                     if(drawing)
  81.                     {
  82.                         Move(drawwin->RPort, mousex, mousey);
  83.                         Text(drawwin->RPort, text, strlen(text));
  84.                         setModified(TRUE);
  85.                     }
  86.                     break;
  87.                 case IDCMP_MENUPICK:
  88.                     going = doMenuPick(drawwin, code);
  89.                     drawwin = getDrawWin();
  90.                     drawsig = 1 << drawwin->UserPort->mp_SigBit;
  91.                     break;
  92.                 }
  93.             }
  94.         }
  95.         /* Now check messages from the tool window */
  96.         if(going && (gotsig & toolsig))
  97.         {
  98.             struct Window* toolwin = getToolWin();
  99.             while(toolwin && (intuimsg = GT_GetIMsg(toolwin->UserPort)))
  100.             {
  101.                 /* Copy the important bits of the message */
  102.                 ULONG class = intuimsg->Class;
  103.                 UWORD code = intuimsg->Code;
  104.                 APTR iaddr = intuimsg->IAddress;
  105.                 /* Reply when finished copying bits from message */
  106.                 GT_ReplyIMsg(intuimsg);
  107.                 /* Act on this message... */
  108.                 switch(class)
  109.                 {
  110.                 case IDCMP_CLOSEWINDOW:
  111.                     closeToolWin();
  112.                     /* Update our local toolwin, so we stop loop */
  113.                     toolwin = NULL;
  114.                     uncheckToolBar(drawwin);
  115.                     break;
  116.                 case IDCMP_REFRESHWINDOW:
  117.                     /* You *MUST* remember to ask for and handle these refresh messages */
  118.                     GT_BeginRefresh(toolwin);
  119.                     GT_EndRefresh(toolwin, TRUE);
  120.                     break;
  121.                 case IDCMP_GADGETUP:
  122.                     doGadgetUp(drawwin, code, (struct Gadget*)iaddr);
  123.                     break;
  124.                 case IDCMP_MENUPICK:
  125.                     going = doMenuPick(drawwin, code);
  126.                     /* Update our local toolwin, so we stop loop */
  127.                     toolwin = getToolWin();
  128.                     drawwin = getDrawWin();
  129.                     drawsig = 1 << drawwin->UserPort->mp_SigBit;
  130.                     break;
  131.                 }
  132.             }
  133.         }
  134.         /* Now check messages from the ARexx port */
  135.         if(going && (gotsig & arexxsig))
  136.         {
  137.             struct RexxMsg* msg;
  138.             while(going && (msg = getARexxMsg()))
  139.                 going = doARexx(msg, drawwin);
  140.         }
  141.     }
  142. }
  143.  
  144. /* Process IDCMP_GADGETUP event */
  145. static void doGadgetUp(struct Window* drawwin, UWORD code, struct Gadget* gad)
  146. {
  147.     switch(gad->GadgetID)
  148.     {
  149.     case MYBUT_ID:
  150.         /* Our button was clicked!  Set foreground to next pen colour */
  151.         nextFgPen(drawwin);
  152.         break;
  153.     case MYPAL_ID:
  154.         /* Our palette gadget was clicked!  Set foreground to gadget colour */
  155.         setFgPen(drawwin, code);
  156.         break;
  157.     }
  158. }
  159.  
  160. /* Process IDCMP_MENUPICK event */
  161. static int doMenuPick(struct Window* drawwin, UWORD code)
  162. {
  163.     UWORD menuCode, menuNumber, itemNumber;
  164.     /* Loop over all the menu selections in the menu code */
  165.     struct MenuItem* item;
  166.     for(menuCode = code;
  167.             menuCode != MENUNULL;
  168.             menuCode = item->NextSelect)
  169.     {
  170.         item = ItemAddress(drawwin->MenuStrip, menuCode);
  171.         /* Extract the menu number and menu item number from the menu code */
  172.         menuNumber = MENUNUM(menuCode);
  173.         itemNumber = ITEMNUM(menuCode);
  174.         /* Now decide what to do based on what menu item was selected */
  175.         switch(menuNumber)
  176.         {
  177.         case 0:  /* Project menu */
  178.             switch(itemNumber)
  179.             {
  180.             case 0:  /* Load */
  181.                 {
  182.                     struct EasyStruct myreq = { sizeof(struct EasyStruct),
  183.                                                                             0,
  184.                                                                             "Overwrite Confirmation",
  185.                                                                             "Do you really wish to overwrite the current image?",
  186.                                                                             "Yes|No" };
  187.                     /* If the image isn't modified we don't need to ask... */
  188.                     if(!isModified() || EasyRequest(drawwin, &myreq, NULL))
  189.                     {
  190.                         return load();
  191.                     }
  192.                 }
  193.                 break;
  194.             case 1:  /* Save */
  195.                 save();
  196.                 break;
  197.             case 3:  /* New (item 2 is the bar!) */
  198.                 {
  199.                     struct EasyStruct myreq = { sizeof(struct EasyStruct),
  200.                                                                             0,
  201.                                                                             "Erase Confirmation",
  202.                                                                             "Do you really wish to clear the current image?",
  203.                                                                             "Yes|No" };
  204.                     /* If the image isn't modified we don't need to ask... */
  205.                     if(!isModified() || EasyRequest(drawwin, &myreq, NULL))
  206.                     {
  207.                         new(drawwin);
  208.                     }
  209.                 }
  210.                 break;
  211.             case 5:  /* About (item 4 is the bar!) */
  212.                 {
  213.                     struct EasyStruct myreq = { sizeof(struct EasyStruct),
  214.                                                                             0,
  215.                                                                             "About",
  216.                                                                             PROGRAM " v" VERSION "\n"
  217.                                                                                 "Brought to you by CU Amiga",
  218.                                                                             "OK" };
  219.                     EasyRequest(drawwin, &myreq, NULL);
  220.                 }
  221.                 break;
  222.             case 7:  /* Quit (item 6 is the bar!) */
  223.                 {
  224.                     struct EasyStruct myreq = { sizeof(struct EasyStruct),
  225.                                                                             0,
  226.                                                                             "Exit Confirmation",
  227.                                                                             "Do you really wish to quit?",
  228.                                                                             "Yes|No" };
  229.                     /* If the image isn't modified we don't need to ask... */
  230.                     if(!isModified() || EasyRequest(drawwin, &myreq, NULL))
  231.                         return FALSE;
  232.                 }
  233.                 break;
  234.             }
  235.             break;
  236.         case 1:  /* Pen menu */
  237.             switch(itemNumber)
  238.             {
  239.             case 0:  /* Next */
  240.                 nextFgPen(drawwin);
  241.                 break;
  242.             case 1:  /* Prev */
  243.                 prevFgPen(drawwin);
  244.                 break;
  245.             case 3:  /* Reset (item 2 is the bar!) */
  246.                 resetFgPen(drawwin);
  247.                 break;
  248.             }
  249.             break;
  250.         case 2:  /* Tools menu */
  251.             switch(itemNumber)
  252.             {
  253.             case 0:  /* Screen Bar */
  254.                 ShowTitle(drawwin->WScreen, item->Flags & CHECKED);
  255.                 break;
  256.             case 1:  /* Tool Bar */
  257.                 /* Do the open or close */
  258.                 if(item->Flags & CHECKED)
  259.                 {
  260.                     /* If the open fails, stop immediately */
  261.                     if(!openToolWin())
  262.                         return FALSE;
  263.                 }
  264.                 else
  265.                     closeToolWin();
  266.                 break;
  267.             case 3:  /* Fractal (item 2 is the bar!) */
  268.                 drawFractal(drawwin);
  269.             }
  270.         }
  271.     }
  272.     /* Keep going */
  273.     return TRUE;
  274. }
  275.  
  276. static void new(struct Window* win)
  277. {
  278.     SetRast(win->RPort, 0);
  279.     setModified(FALSE);
  280. }
  281.  
  282. /* Our RDArgs structure for use with ReadArgs() */
  283. static struct RDArgs* myrdargs = NULL;
  284.  
  285. int createArgs()
  286. {
  287.     if(myrdargs = AllocDosObject(DOS_RDARGS, NULL))
  288.     {
  289.         /* Disable prompting on stdin when "?" is the argument */
  290.         myrdargs->RDA_Flags = RDAF_NOPROMPT;
  291.         return TRUE;
  292.     }
  293.     else
  294.         printf("Error: could not allocate args for ARexx commands\n");
  295.     return FALSE;
  296. }
  297.  
  298. void freeArgs()
  299. {
  300.     if(myrdargs)
  301.         FreeDosObject(DOS_RDARGS, myrdargs);
  302. }
  303.  
  304. /* The result of a ReadArgs() while parsing commands */
  305. static struct RDArgs* rdargs = NULL;
  306.  
  307. /* Test if a string matches a command, using ReadArgs() */
  308. static int isCommand(char* text, char* comm,
  309.                                          char* templ, LONG* args)
  310. {
  311.     int clen = strlen(comm);
  312.     if(strnicmp(text, comm, clen) == 0)
  313.     {
  314.         /* Is the command followed by some whitespace? */
  315.         if(text[clen] == ' ' || text[clen] == '\t')
  316.         {
  317.             int tlen = strlen(text);
  318.             /* Set up our myrdargs so we can use ReadArgs() */
  319.             myrdargs->RDA_Source.CS_Buffer = text+clen+1;
  320.             myrdargs->RDA_Source.CS_Length = tlen-clen;
  321.             myrdargs->RDA_Source.CS_CurChr = 0;
  322.             myrdargs->RDA_DAList = NULL;
  323.             myrdargs->RDA_Buffer = NULL;
  324.             /* Temporarily end the string with a return... */
  325.             /* (Needed to get ReadArgs() to work properly) */
  326.             text[tlen] = '\n';
  327.             rdargs = ReadArgs(templ, args, myrdargs);
  328.             /* ... now we must reinstate the string's terminator */
  329.             text[tlen] = '\0';
  330.             return rdargs != NULL;
  331.         }
  332.     }
  333.     return NULL;
  334. }
  335.  
  336. static void freeCommand()
  337. {
  338.     if(rdargs)
  339.     {
  340.         FreeArgs(rdargs);
  341.         rdargs = NULL;
  342.     }
  343. }
  344.  
  345. /* The maximum number of arguments for our commands */
  346. #define MAX_ARGS (3)
  347.  
  348. #define COMM_QUIT        "QUIT"
  349.  
  350. #define COMM_NEW        "NEW"
  351.  
  352. #define COMM_PEN        "PEN"
  353. #define TEMPL_PEN        "PEN/N"
  354. enum PEN_ARGS { PEN_PEN };
  355.  
  356. #define COMM_DRAW        "DRAW"
  357. #define TEMPL_DRAW    "X/N,Y/N,TEXT/F"
  358. enum DRAW_ARGS { DRAW_X, DRAW_Y, DRAW_TEXT };
  359.  
  360. /* Process an ARexx message */
  361. static int doARexx(struct RexxMsg* msg, struct Window* drawwin)
  362. {
  363.     int going = TRUE;
  364.     /* By default, our reply will indicate an error */
  365.     LONG rc = 20;
  366.     char* res = NULL;
  367.     char* command = msg->rm_Args[0];
  368.     /* Parse the command */
  369.     if(stricmp(command, COMM_QUIT) == 0)
  370.     {
  371.         going = FALSE;
  372.         /* We recognised the command, so set rc to zero */
  373.         rc = 0;
  374.         res = "Hello Painter is quitting";
  375.     }
  376.     else if(stricmp(command, COMM_NEW) == 0)
  377.     {
  378.         new(drawwin);
  379.         rc = 0;
  380.         res = "Display cleared";
  381.     }
  382.     else
  383.     {
  384.         LONG args[MAX_ARGS];
  385.         int i;
  386.         for(i=0; i<MAX_ARGS; i++)
  387.             args[i] = NULL;
  388.         if(isCommand(command, COMM_PEN, TEMPL_PEN, args))
  389.          {
  390.             /* args[0] holds the pen number to use */
  391.             LONG* nptr = (LONG*)(args[PEN_PEN]);
  392.             setFgPen(drawwin, *nptr);
  393.             rc = 0;
  394.             res = "Pen set";
  395.         }
  396.         else if(isCommand(command, COMM_DRAW, TEMPL_DRAW, args))
  397.          {
  398.             /* args[DRAW_X] and args[DRAW_Y] hold the coordinate */
  399.             /* args[DRAW_TEXT] holds the text to be drawn */
  400.             LONG* xptr = (LONG*)(args[DRAW_X]);
  401.             LONG* yptr = (LONG*)(args[DRAW_Y]);
  402.             char* text = (char*)(args[DRAW_TEXT]);
  403.             Move(drawwin->RPort, *xptr, *yptr);
  404.             Text(drawwin->RPort, text, strlen(text));
  405.             setModified(TRUE);
  406.             rc = 0;
  407.             res = "Text drawn";
  408.         }
  409.         freeCommand();
  410.     }
  411.     replyARexxMsg(msg, rc, res);
  412.     return going;
  413. }
  414.